package com.ruoyi.framework.aspectj;


import com.ruoyi.framework.web.SysUserUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 请求响应日志拦截打印
 */
@Aspect
@Component
public class WebLogAspect {

    /**
     * 记录
     */
    private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    /**
     * 控制器日志
     */
    @Pointcut("execution(public * com.ruoyi.*.controller..*.*(..))")
    public void controllerLog() {
    }

    /**
     * 控制器前日志
     *
     * @param joinPoint 加入点
     */
    @Before("controllerLog()")
    public void logBeforeController(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //这个RequestContextHolder是Springmvc提供来获得请求的东西
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\n===============拦截请求相关数据===============");
        stringBuilder.append("\nURL : ");
        stringBuilder.append(request.getRequestURL().toString());
        stringBuilder.append("\nHTTP_METHOD : ");
        stringBuilder.append(request.getMethod());
        stringBuilder.append("\nUSER : ");
        stringBuilder.append(SysUserUtil.getUserName() == null ? "未登录" : SysUserUtil.getUserName());
        stringBuilder.append("\nIP : ");
        stringBuilder.append(request.getRemoteAddr());
        stringBuilder.append("\nTHE ARGS OF THE CONTROLLER : ");
        stringBuilder.append(Arrays.toString(joinPoint.getArgs()));
        stringBuilder.append("\nCLASS_METHOD : ");
        stringBuilder.append(joinPoint.getSignature().getDeclaringTypeName());
        stringBuilder.append(".");
        stringBuilder.append(joinPoint.getSignature().getName());
        stringBuilder.append("\n===========================================");
        logger.info(stringBuilder.toString());
    }

    /**
     * 返回后再做
     *
     * @param joinPoint 加入点
     * @param returnOb  返回 OB
     */
    @AfterReturning(returning = "returnOb", pointcut = "controllerLog()")
    public void doAfterReturning(JoinPoint joinPoint, Object returnOb) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\n===============拦截返回相关数据===============");
        stringBuilder.append("\nTHE RETURN OF THE METHOD IS : ");
        stringBuilder.append(returnOb);
        stringBuilder.append("\n===========================================");
        logger.debug(stringBuilder.toString());
    }

}